package com.wanpinghui.wphlibrary.model.base;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import com.wanpinghui.wphlibrary.R;
import com.wanpinghui.wphlibrary.WPHLibrary;
import com.wanpinghui.wphlibrary.common.IWPHApi;
import com.wanpinghui.wphlibrary.model.base.bean.AppInfoBean;
import com.yunshl.yunshllibrary.manager.UpdateManager;
import com.yunshl.yunshllibrary.storage.StorageManager;
import com.yunshl.yunshllibrary.utils.DevicesUtil;
import com.yunshl.yunshllibrary.utils.FileUtil;
import com.yunshl.yunshllibrary.utils.LogUtils;
import com.yunshl.yunshllibrary.utils.TextUtil;

import retrofit.Network;
import retrofit.callback.YRequestCallback;
import retrofit.callback.YunShlAction;
import retrofit.callback.YunShlResult;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/12/4
 ***/

public class AppUpdateManager {
    private UpdateManager manager;
    public AppUpdateManager(Activity context) {
        manager = UpdateManager.newInstance();
    }

    public AppUpdateManager() {
        manager = UpdateManager.newInstance();
    }

    public void checkVersion(final YRequestCallback<AppInfoBean> callback) {
        Network.getApi(IWPHApi.class)
                .checkAppVersion()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<YunShlResult<AppInfoBean>>() {
                    @Override
                    public void call(YunShlResult<AppInfoBean> result) {
                        if (result.isSuccess()) {
                            if (callback != null) {
                                callback.onSuccess(result.body);
                            }
                        }else {
                            if (callback != null) {
                                callback.onFailed(result.status,result.msg);
                            }
                        }
                    }
                }, new YunShlAction<Throwable>(callback));
    }

    public void downloadApp(String url, UpdateManager.IDownloadStatus downloadStatus) {
        manager.setDownloadStatus(downloadStatus);
        manager.downLoad(url, StorageManager.getFileDir()+"/app","apk");
    }

    public void download(String url, UpdateManager.IDownloadStatus downloadStatus) {
        manager.setDownloadStatus(downloadStatus);
        manager.downLoad(url, StorageManager.getMediaPath(),url.substring(url.lastIndexOf(".")+1));
    }

    public void download(String url, String path, UpdateManager.IDownloadStatus downloadStatus) {
        manager.setDownloadStatus(downloadStatus);
        manager.downLoad(url, StorageManager.getMediaPath(),url.substring(url.lastIndexOf(".")+1));
    }

    public static boolean haveNewVersion(AppInfoBean appInfoBean) {
        if (appInfoBean == null) {
            return false;
        }else {
            String version = DevicesUtil.getVersion(WPHLibrary.getLibrary().getContext());
            if (version.compareTo(appInfoBean.getVersion()) < 0) {
                return true;
            }
            if (version.compareTo(appInfoBean.getVersion()) == 0) {
                StorageManager.deleteDirectory(StorageManager.getFileDir()+"/app");
            }
        }
        return false;
    }


}
